package com.fanfutian.eventproject.service.impl;

import com.fanfutian.eventproject.mapper.ArticleMapper;
import com.fanfutian.eventproject.pojo.Article;
import com.fanfutian.eventproject.pojo.PageBean;
import com.fanfutian.eventproject.service.ArticleService;
import com.fanfutian.eventproject.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * 文章业务实现类
 * @author fanfutian
 */
@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 添加文章
     * @param article
     */
    @Override
    public void addArticle(Article article) {
        // 部分字段赋值
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        articleMapper.addArticle(article);
    }



    /**
     * 文章列表
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        // 1、创建PageBean对象
        PageBean<Article> pageBean = new PageBean<>();

        // 2、开启分页查询（Spring的插件）
        PageHelper.startPage(pageNum,pageSize);

        // 3、调用持久层方法（根据当前用户的ID查询当前用户的文章列表）
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        // 查询当前用户的文章列表
        List<Article> articleList = articleMapper.list(categoryId,state,userId);
        // Page中提供了方法，可以获取PageHelper分页查询后的数据,所以将list列表强转为Page
        Page<Article> articlePage = (Page<Article>) articleList;
        // 将数据填充到pageBean中
        pageBean.setTotal(articlePage.getTotal());
        pageBean.setItems(articlePage.getResult());

        // 4、返回数据
        return pageBean;
    }



    /**
     * 根据文章ID查询文章详情
     * @param id
     * @return
     */
    @Override
    public Article getArticleById(Integer id) {
        // 查询时需要验证文章是否属于当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        Article article = articleMapper.detail(id, userId);
        return article;
    }


    /**
     * 修改文章信息
     * @param article
     */
    @Override
    public void updateArticle(Article article) {
        // 部分字段赋值
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        article.setCreateUser(userId);
        article.setUpdateTime(LocalDateTime.now());
        articleMapper.updateArticle(article);
    }


    /**
     * 删除文章
     * @param id
     */
    @Override
    public void deleteArticle(Integer id) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        articleMapper.deleteArticle(id,userId);
    }
}
